function randomNum(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}

let start = false;
let c_w = 400;
let c_h = 400;
let main = document.getElementById('abs');
main.setAttribute('style', `width:${c_w}px;height:${c_h}px;border:1px solid;position: relative;`);
var left = 50;
var ttp = 50;
let snake_w = 10;
let snake_h = 10;

let les = [];

let snakeBodyList = [];
let render = function () {
    for (let o of les) {
        main.removeChild(o);
    }
    les = [];
    for (let i of snakeBodyList) {
        let item = document.createElement('div');
        item.setAttribute('style', `background-color:red;width:${snake_w}px;height:${snake_h}px;position: absolute;left: ${i[0] * snake_w}px;top: ${i[1] * snake_h}px;`);
        main.appendChild(item);
        les.push(item);
    }
};
let over = function () {
    start = false;
    console.log('游戏结束');
    console.log(`你的分数是：${snakeBodyList.length}`);
    main.removeChild(m.el);
}
let key = 's';
let move = function () {
    if (!start) {
        return;
    }
    let first = snakeBodyList[0];
    let x = first[0];
    let y = first[1];
    let maxw = (c_w - snake_w) / snake_w;
    let maxh = (c_h - snake_h) / snake_h;

    switch (key) {
        case "w":
            if (y <= 0) {
                over();
                return;
            }
            y = y - 1;
            break;
        case "a":
            if (x <= 0) {
                over();
                return;
            }
            x -= 1;

            break;
        case "s":
            if (y >= maxh) {
                over();
                return;
            }
            y += 1;
            break;
        case "d":
            if (x >= maxw) {
                over();
                return;
            }
            x += 1;
            break;
    }
    let iseat = false;
    if (x == m.x && y == m.y) {
        iseat = true;
        console.log('吃到老鼠了');
        snakeBodyList.unshift([x, y]);
        main.removeChild(m.el);
        m = buildM();
    }
    if (iseat == false) {
        snakeBodyList.pop();
        let finditem = snakeBodyList.find(function (c) {
            if (c[0] == x && c[1] == y) {
                return true;
            }
            return false;
        });
        if (finditem != undefined) {
            console.log('你死了');
            start = false;
            main.removeChild(m.el);
            return;
        }
        snakeBodyList.unshift([x, y]);
    }


    render();
}

setInterval(move, 100);


document.body.addEventListener('keydown', function (e) {
    if (e.key == 'w' && key != 's') {
        key = e.key;
    }
    if (e.key == 'a' && key != 'd') {
        key = e.key;
    }
    if (e.key == 's' && key != 'w') {
        key = e.key;
    }
    if (e.key == 'd' && key != 'a') {
        key = e.key;
    }
});
let m;

let buildM = function () {
    let mouse_w = 10;
    let mouse_h = 10;
    let maxw = c_w / mouse_w;
    let maxh = c_h / mouse_h;
    let mouse_x = randomNum(0, maxw);
    let mouse_y = randomNum(0, maxh);
    let mm = document.createElement("div");
    mm.setAttribute('style', `left:${mouse_x * mouse_w}px;top:${mouse_y * mouse_h}px;width:${mouse_w}px;height:${mouse_h}px;background-color:blue;position: absolute;`);

    main.appendChild(mm);
    return {x: mouse_x, y: mouse_y, el: mm};
};

function gamestart() {
    if(start){
        return;
    }
    key='s';
    snakeBodyList = [[2, 2], [2, 3]];
    render();
    m = buildM();
    start = true;
}

document.getElementById('asd').addEventListener('click', gamestart);
